//外接询价单列表

interface ICspInquiryViewModel extends IBaseViewModel<ICspInquiryQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<CspInquiryListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 列表高度
     */
    heightA:number;
    /**
     * 询价状态下拉
     */
    inquiryStatusDropDown: ValueListItemResponse[];
    /**
     * 询价单编辑
     */
    editInquiry: (id:string) => void;
     /**
     * 询价单查看详情
     */
   detailInquiry: (id:string) => void;
   
}

/**
 * 查询条件
 */
interface ICspInquiryQueryTerms {
    /**
     * 总询价单编号
     */
    cspInquiryCode: string;
    /**
     * 子询价单编号
     */
    cspInquiryChildCode: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 询价状态
     */
    states: string;
    /**
     * 下单起始时间
     */
    beginTime: string;
    /**
     * 下单结束时间
     */
    endTime: string;
    /**
     * 发货地址
     */
    beginAddress: string;
    /**
     * 送货地址
     */
    endAddress: string;
}

class cspInquiryController extends ListController<ICspInquiryViewModel, ICspInquiryQueryTerms> {
    gridApi: uiGrid.IGridApiOf<CspInquiryListItemResponse>;
  
    selectedId: string;

    constructor($scope: ICspInquiryViewModel, 
    private cspInquiryService: ICspInquiryService, 
    private valueService: IValueService, 
    private $ngBootbox: BootboxService, 
    private $state: angular.ui.IStateService,
    private $location:ng.ILocationService,
    private exportService:IexportService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
       
        this.$scope.editInquiry = this.editInquiry;//外接询价单编辑

        this.$scope.detailInquiry = this.detailInquiry;//外接询价单详情
     
        this.$scope.$on("WildDog:Order22", this.onWilddogOrderEvent);
        this.$scope.queryTerms = {
            cspInquiryCode: "",
            cspInquiryChildCode: "",
            clientName: "",
            states: "0",
            beginTime: "",
            endTime: "",
            beginAddress: "",
            endAddress: "",
        };

        this.$scope.gridOptions = {
            enableSelectAll: false,
            multiSelect:false,
            enableFullRowSelection: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "总询价编号", field: 'cspInquiryCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "子询价编号", field: 'cspInquiryChildCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '12%' },
                { displayName: '发货地址', field: 'startAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '12%' },
                { displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '12%' },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '货物数量', field: 'quantityOfGoods', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '7%' },
                { displayName: '询价时间', field: 'cspInquiryTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: '紧急程度', field: 'responseTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '询价状态', field: 'status', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<div ng-if="row.entity.status == \'待处理\'">'
                    + '<a class="fa fa-edit m-l-xs primary text-info" title="编辑" ng-click="grid.appScope.editInquiry(row.entity.id)" ></a>'
                    + '</div>'
                    + '<div ng-if="row.entity.status != \'待处理\'">'
                    + '<a class="primary fa fa-eye text-info m-l-xs" title="查看详情" ng-click="grid.appScope.detailInquiry(row.entity.id)"></a>'
                    + '</div>',
                    enableColumnMenu: false, enableSorting: false, width: '4%'
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                // gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
            }
        };
        this.query();
        this.loadInquiryStatusDropDown();
        $('#opendataTime').click(() => {
            $('#dataTime').datetimepicker('show');
        });

        $('#opendataTimeEnd').click(()=> {
            $('#dataTimeEnd').datetimepicker('show');
        });
    }

    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    /**
     * 加载数据
     */
    loadData(queryTerms: ICspInquiryQueryTerms, skip: number, count: number): void {
         if(this.$scope.queryTerms.states == "" || !this.$scope.queryTerms.states){this.$scope.queryTerms.states = "";}
        let states = this.$scope.queryTerms.states
       
        this.cspInquiryService.getList(queryTerms.cspInquiryCode, queryTerms.cspInquiryChildCode,queryTerms.clientName,states,queryTerms.beginTime, queryTerms.endTime, queryTerms.beginAddress, queryTerms.endAddress,  skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                /**
                 * 设置列表高度
                 */
                this.$scope.heightA = (this.$scope.gridOptions.data.length*39) + 80;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 监听野狗事件
     */
    onWilddogOrderEvent = (event: ng.IAngularEvent, data: any) => {
        this.query();
    }

    /**
     * 询价状态下拉
     */
    loadInquiryStatusDropDown(): void {
        this.$scope.inquiryStatusDropDown = this.valueService.getCspStatus().data;
    }

    // /**
    //  * 单选事件
    //  */
    // selectChange = (row: uiGrid.IGridRowOf<CspInquiryListItemResponse>) => {
    //     if(row.isSelected){
    //         this.selectedId = row.entity.id
    //     }
    //     else{
    //         this.selectedId = "";
    //     }
    // }

    /**
     * 外接询价单编辑
     */
    editInquiry = (id:string) => {
        this.$state.go("app.inquiry.external_inquiry_edit", { id: id, name: 'edit' })
    }
    /**
     * 外接询价单查看详情
     */
    detailInquiry = (id:string) => {
        this.$state.go("app.inquiry.external_inquiry_edit", { id: id, name: 'detail' })
    }

    /**
    * 外接询价单管理导出
    */
    exportFunc=()=>{
        if(this.$scope.queryTerms.states == "" || !this.$scope.queryTerms.states){this.$scope.queryTerms.states = "";}
        let states = this.$scope.queryTerms.states

        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportCspinquiryList(this.$scope.queryTerms.cspInquiryCode, this.$scope.queryTerms.cspInquiryChildCode,this.$scope.queryTerms.clientName, states, this.$scope.queryTerms.beginTime, this.$scope.queryTerms.endTime,this.$scope.queryTerms.beginAddress, this.$scope.queryTerms.endAddress,0,-1)
        }  
    }

}

angular.module('tms').controller("cspinquiryController", ["$scope","cspInquiryService","valueService","$ngBootbox","$state",'$location',"exportService", cspInquiryController]);